window.onload=function () {
    var judge = true;
    if (localStorage.getItem('userHao') == null) {
        window.location = 'http://localhost:8080/future/admin/allMusic';
    }
    var music = document.getElementsByClassName('music')[0];
    var musicForm = document.getElementsByClassName('musicForm')[0];
    music.onclick = function () {
        if (judge == true) {
            musicForm.style.display = 'block';
            judge = false;
            console.log();
        } else {
            musicForm.style.display = 'none';
            judge = true;
        }
    }


    //头像处的列表的出现
    var headPhoto = document.getElementsByClassName('headPhoto')[0];
    var personage = document.getElementsByClassName('personage')[0];
    var xia2 = document.getElementsByClassName('xia2')[0];
    headPhoto.innerHTML = localStorage.getItem('userHao');
    headPhoto.onclick = function () {
        if (judge == true) {
            console.log("aaaaa");
            personage.style.height = '8%';
            judge = false;
        } else {
            personage.style.height = '0%';
            judge = true;
        }

    }


    //退出登录
    var back = document.getElementsByClassName('back')[0];
    back.onclick = function () {
        var flag = confirm("是否确定要退出登录", "确定");
        if (flag) {
            window.location.href = 'http://localhost:8080/future/loginpage';
            localStorage.removeItem('userHao');//清除保留的用户名
        }

    }

    //查询歌曲
    var input = document.getElementsByClassName('input')[0];//用户名的输入框
    var ssBtn = document.getElementsByClassName('ssBtn')[0];//搜索的按钮
    var Apage=document.getElementsByClassName('pageAll')[0];
    ssBtn.onclick = function () {
        $.ajax({
            url: 'http://localhost:8080/future/admin/querySongBymcName',
            method: "GET",
            data: {//参数
                mcName: input.value,
            },
            success: function (data) {
            if(data.length===0){
                alert("查无此信息！");
                location.reload();
            }else {


                console.log(data);
                document.getElementsByClassName('content')[0].innerHTML = '';
                // console.log(data.length);
                var a=data.length;
                Math.ceil(a / 10); //向上整除 4/3=2;
                console.log(  Math.ceil(a / 10));

                for (var index = 0; index < data.length; index++) {
                    var element = data[index];
                    console.log(element);
                    var i = index + 1;
                    document.getElementsByClassName("content")[0].innerHTML += "<tr class='found'>" + "<td class='find'>" + "<input class='choice' type='checkbox' name='ids'>" + "</td>" + "<td class='find'>" + i + "</td>" + "<td class='find'>" + element.mcName + "</td>" + "<td class='find'>" + element.mcDown + "</td>" + "<td class='find'>" + element.mcLv + "</td>" + "<td class='find'>" + element.mcTime + "</td>" +
                        "<td class='find'>" + element.albName + "</td>" + "<td class='find'>" + element.userName + "</td>" +
                        "<td class='find'>" + "<button class='del'>删除</button>" + "<button class='amend'>更改</button>" + "</td>" + "</tr>"

                }
                Apage.innerHTML=  Math.ceil(a / 10);

            }
            },
            error: function (err) {//请求失败，执行的回调函数
                console.log('错误');
            }
        })
    }




    // 新增的弹窗
    var addUser = document.getElementsByClassName('addUser')[0];
    var add = document.getElementsByClassName('add')[0];
    var affirm = document.getElementsByClassName('affirm')[0];
    var cancel = document.getElementsByClassName('cancel')[0];
    var reset = document.getElementsByClassName('reset')[0];
    add.onclick = function () {
        addUser.style.display = 'block';
    }
    affirm.onclick = function () {
        addUser.style.display = 'none';
    }
    cancel.onclick = function () {
        addUser.style.display = 'none';
    }
    reset.onclick = function () {
        window.location.href = "http://localhost:8080/future/admin/allMusic";//页面刷新
    }


    //新增一首歌曲
    var affirm_3 = document.getElementsByClassName('affirm_3')[0];
    var addalbId = document.getElementsByClassName('addalbId')[0];
    var addalbName = document.getElementsByClassName('addalbName')[0];
    var addHot = document.getElementsByClassName('addHot')[0];
    var addLrc = document.getElementsByClassName('addLrc')[0];
    var addsongName = document.getElementsByClassName('addsongName')[0];
    var addMv = document.getElementsByClassName('addMv')[0];
    var addSinger = document.getElementsByClassName('addSinger')[0];
    var selectmcLv=document.getElementsByClassName('selectmcLv')[0];
    // var load=document.getElementsByClassName('load')[0];


    // console.log( selectmcLv.value);
    affirm_3.onclick = function () {
        let formData=new FormData();
        formData.append("file",$('#aload').get(0).files[0]);
        formData.append("albId",addalbId.value);
        formData.append("albName",addalbName.value);
        formData.append("mcHot",addHot.value);
        formData.append("mcLrc",addLrc.value);
        formData.append("mcLv",selectmcLv.value);
        formData.append("mcName",addsongName.value);
        formData.append("mcMvLoad",addMv.value);
        formData.append("userName",addSinger.value);

        console.log(addalbId.value);
        console.log($('.load').get(0).files[0]);
        console.log(addalbName.value);
        console.log(addHot.value);
        console.log(addLrc.value);
        console.log(selectmcLv.value);
        console.log(addMv.value);
        console.log(addsongName.value);
        console.log(addSinger.value);
        console.log("aaaaaaaaaaaa");
        // console.log(load.value);
        console.log(formData);
        $.ajax({
            url: 'http://localhost:8080/future/admin/addSong',
            method: 'POST',
            data: formData,
            cache:false,
            contentType:false,  // 不用任何编码 因为formdata对象自带编码 django能够识别该对象
            processData:false,  // 告诉浏览器不要处理我的数据 直接发就行
            success: function (data) {
                console.log(data);//控制台显示返回的数据
                location.reload();//添加成功则进行页面刷新

            },
            error: function (err) {//请求失败，执行的回调函数
                console.log('错误');
            }

        })
    }
    //删除一首音乐

    var del = document.getElementsByClassName('del');
    for (var i = 0; i < del.length; i++) {
        del[i].onclick = function () {
            // console.log(this.parentNode.parentNode.getAttribute("everid"));//获取当前行的id
            var flag = confirm("是否要删除该首音乐", "确定");
            // var xuHao = this.parentNode.parentNode.children[1].innerHTML;
            if (flag) {

                $.ajax({
                    url: "http://localhost:8080/future/admin/deleteSong",
                    type: "GET",
                    data: {
                        mcId: this.parentNode.parentNode.getAttribute("everid"),
                    },
                    success: function (data) {
                        if (data.statusCode === 1) {

                         alert("删除成功！");
                         location.reload();


                            alert("删除成功")
                            window.location.reload();
                        } else {
                            alert("删除失败");
                            // console.log(data.info);
                        }
                    },
                    error: function (err) {
                        console.log("错误");
                    },
                })
            }
        }
    }

//修改音乐信息
    var change=document.getElementsByClassName('amendUser')[0];
    var affirm_2 = document.getElementsByClassName('affirm_2')[0];
    var cancel_2 = document.getElementsByClassName('cancel_2')[0];
    var information_2=document.getElementsByClassName('information_2');
    var amend=document.getElementsByClassName('amend');
    for(var i=0;i < amend.length;i++){
        amend[i].onclick=function (){
            information_2[0].value=this.parentNode.parentNode.getAttribute("albId");
            information_2[1].value=this.parentNode.parentNode.children[6].innerHTML;
            information_2[4].value=this.parentNode.parentNode.children[4].innerHTML;
            information_2[5].value=this.parentNode.parentNode.children[2].innerHTML;
            information_2[6].value=this.parentNode.parentNode.getAttribute("everId");
            information_2[7].value=this.parentNode.parentNode.children[7].innerHTML;
            information_2[2].value=this.parentNode.parentNode.getAttribute("mcHot");
            information_2[3].value=this.parentNode.parentNode.getAttribute("mcLrc");

            change.style.display = 'block';
            affirm_2.onclick = function () {
                var flag_1 = confirm("是否确认要修改当前音乐的信息", "确认");
                if (flag_1) {
                    alter();
                    change.style.display = 'none';
                    location.reload();
                } else {
                    change.style.display = 'none';
                    location.reload();
                }

            }
            cancel_2.onclick = function () {
                change.style.display = 'none';
                location.reload();
            }

            function alter() {
                $.ajax({
                    url: 'http://localhost:8080/future/admin/updateSong',
                    method: 'GET',
                    data: {
                        albId:information_2[0].value,
                        albName:information_2[1].value,
                        mcHot:information_2[2].value,
                        mcLrc:information_2[3].value,
                        mcLv:information_2[4].value,
                        mcName:information_2[5].value,
                        mcId:information_2[6].value,
                        userName:information_2[7].value,
                    },
                    dataType: "json",
                    success: function (data) {

                        // location.reload();
                        console.log(data);
                        // if(data.statusCode==0){
                        //     alter(data.info);
                        // }
                    },
                    error: function (err) {//请求失败，执行的回调函数
                        console.log('错误');
                    }
                })
            }

        }
    }




    //全选
    //点击获取下面全部的信息，再点取消
    var all = document.getElementById('all');

    function DoCheck() {
        var ch = document.getElementsByName("ids");
        if (document.getElementsByName("allChecked")[0].checked == true) {
            for (var i = 0; i < ch.length; i++) {
                ch[i].checked = true;
            }
        } else {
            for (var i = 0; i < ch.length; i++) {
                ch[i].checked = false;
            }
        }
    }

    all.onclick = DoCheck;

    //分页查询
    var previous = document.getElementsByClassName('previous')[0];
    var next = document.getElementsByClassName('next')[0];
    var page = 1;
    var toPage = document.getElementsByClassName('toPage')[0];
    var pageAll = document.getElementsByClassName('pageAll')[0].innerHTML
    var first = document.getElementsByClassName('first')[0];
    var end = document.getElementsByClassName('end')[0];
    first.onclick = function () {
        location.href = 'http://localhost:8080/future/admin/allMusic?page=1';
    }
    end.onclick = function () {
        location.href = 'http://localhost:8080/future/admin/allMusic?page=' + pageAll;
    }
    previous.onclick = function () {
        if (toPage.value <= 1) {
            alert("这已经是最前面一页！");
            return;
        } else {
            page = toPage.value - 1;
            location.href = 'http://localhost:8080/future/admin/allMusic?page=' + page;
        }

    }
    next.onclick = function () {
        console.log(pageAll);
        var pagenum = pageAll;
        if (Number(toPage.value) >= pagenum) {
            alert("没有下一页了！");
            return;
        } else {
            page = Number(toPage.value) + 1;

            location.href = 'http://localhost:8080/future/admin/allMusic?page=' + page;
            console.log(page);
        }
    }

    //更改页码框中的值
    toPage.onkeydown = function (event) {
        var event = event;
        // console.log(event.keyCode)
        if (event.keyCode == 13) {
            location.href = 'http://localhost:8080/future/admin/allMusic?page=' + Number(toPage.value);
        }
    }

//批量删除
    $(function(){
        $(".batch").click(function(){
            //判断是否至少选择一项
            var checkedNum = $("input[name='ids']:checked").length;
            console.log(checkedNum);
            if(checkedNum==0){
                alert("请至少选择一项进行删除");
                return;
            }
            if(confirm("确定要删除所选项目？")){
                //这个数组存储要删除的数据的id
                var checkedList = [];
                //每一个被选中的name为ids的input标签
                $("input[name='ids']:checked").each(function(){
                    checkedList.push(String($(this).val()));//将id存入数组
                    console.log(JSON.stringify(checkedList));

                });
                $.ajax({
                    type:"GET",
                    dataType:"json",
                    // contentType:"application/json",
                    url:"http://localhost:8080/future/admin/BatchdeleteSong",//不同页面只用更改action即可
                    async:true,
                    //数组的toString里的id是用","分隔开的，例如数组{"1","2","3"},同String后为"1,2,3"
                    data:{
                        numbers:JSON.stringify(checkedList),
                    },
                    success:function(data){
                        console.log(data);
                        alert(data.info);
                        window.location.reload();
                        // if(data==1){
                        //     for(var i=0;i<checkedList.length;i++){
                        //         $("#tr_"+checkedList[i]).remove();//ajax动态移除
                        //     }
                        //     alert("删除成功");
                        // }else{
                        //     alert("删除失败");
                        // }
                    },
                    error:function(){
                        alert("错误");
                    }
                })
            }
        })

    })



}